package com.easylinkin.linkappapi.deviceattribute.controller;

import com.easylinkin.linkappapi.annotation.CommonOperateLogAnnotate;
import com.easylinkin.linkappapi.operatelog.constant.LogConstant.LogModule;
import java.util.List;

import javax.annotation.Resource;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.easylinkin.linkappapi.deviceattribute.entity.DeviceAttribute;
import com.easylinkin.linkappapi.deviceattribute.service.DeviceAttributeService;

import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author TongJie
 * @since 2020-04-15
 */
@RestController
@Api(value="设备属性控制器",tags={"设备属性控制器"})
@RequestMapping("/deviceattribute")
public class DeviceAttributeController {

    @Resource
    private DeviceAttributeService deviceAttributeService;

    @ApiOperation("根据查询条件获取全部设备属性列表")
    @PostMapping("getAll")
    public RestMessage getAll(@RequestBody DeviceAttribute deviceAttribute) {
        return RestBuilders.successBuilder(deviceAttributeService.getAll(deviceAttribute)).build();
    }

    @ApiOperation("根据设备属性ID批量修改设备属性")
    @PostMapping("/update")
    @CommonOperateLogAnnotate(module = LogModule.DEVICE_ATTRIBUTE, desc = "修改设备属性")
    public RestMessage update(@RequestBody List<DeviceAttribute> deviceAttributes) {
        Assert.noNullElements(deviceAttributes, "设备属性为空");
        return RestBuilders.successBuilder().data(deviceAttributeService.updateBatchById(deviceAttributes)).build();
    }

    @ApiOperation("根据ID单个修改设备属性")
    @PostMapping("/updateDeviceAttribute")
    @CommonOperateLogAnnotate(module = LogModule.DEVICE_ATTRIBUTE, desc = "修改设备属性")
    public RestMessage updateDeviceAttribute(@RequestBody DeviceAttribute deviceAttributes) {
        Assert.notNull(deviceAttributes, "设备属性为空");
        UpdateWrapper updateWrapper = new UpdateWrapper();
        
        if(null != deviceAttributes.getIcoPath()) {
        	updateWrapper.set("ico_path", deviceAttributes.getIcoPath());
        }
        if(null != deviceAttributes.getIsShow()) {
        	updateWrapper.set("is_show", deviceAttributes.getIsShow());
        	updateWrapper.eq("parent_id", deviceAttributes.getId());
        	deviceAttributeService.update(updateWrapper);
        }
        return RestBuilders.successBuilder().data(deviceAttributeService.updateById(deviceAttributes)).build();
    }

    /**
     * 和 getAll 此接口完全一样
     * @param deviceAttribute
     * @return
     */
    @ApiOperation("根据查询条件获取全部设备属性列表")
    @PostMapping("getProps")
    public RestMessage getProps(@RequestBody DeviceAttribute deviceAttribute) {
        Assert.notNull(deviceAttribute, "参数为空");
        return RestBuilders.successBuilder().data(deviceAttributeService.getAll(deviceAttribute)).build();
    }
    
}
